package com.site.util;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.codehaus.plexus.util.StringUtils;

import com.site.util.extremetable.QueryPage;


/**
 * 前台用于翻页的小标签，只能上一页和下一页
 * @author 冉可
 *
 */
public class FoodPageTag extends TagSupport {

	private static final long serialVersionUID = 1L;

	private QueryPage queryPage;
	
	private String action;

	private String namespace;
	
	private Map<String,String> paramMap=new HashMap<String,String>();
	
	@Override
	public int doStartTag() throws JspException{
		queryPage=(QueryPage)pageContext.getRequest().getAttribute("queryPage");
		paramMap.clear();
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doEndTag() throws JspException {
		try {
			if(queryPage.getTargetPage()>1) {
			
				pageContext.getOut().write("<li class=\"prev_page\"><a style=\"padding:0;\" href=\""+getPreviousPagePath()+"\"><img src=\"../images/food/list/icon6.jpg\"  width=\"21\" height=\"17\"/></a></li>");
			} else {
				pageContext.getOut().write("<li class=\"prev_page\"><img src=\"../images/food/list/icon62.jpg\"  width=\"21\" height=\"17\"/></li>");
			
			}
		
			if(queryPage.getPageCount()==0)
				pageContext.getOut().write("<li class=\"number\">"+queryPage.getTargetPage()+"/1</li>");
			else
				pageContext.getOut().write("<li class=\"number\">"+queryPage.getTargetPage()+"/"+queryPage.getPageCount()+"</li>");
			if(queryPage.getTargetPage()<queryPage.getPageCount()) {
				pageContext.getOut().write("<li class=\"prev_page2\"><a style=\"padding:0;\" href=\""+getNextPagePath()+"\" ><img src=\"../images/food/list/icon5.jpg\"  width=\"21\" height=\"17\"/></a></li>");
			} else {
				pageContext.getOut().write("<li class=\"prev_page2\"><img src=\"../images/food/list/icon52.jpg\"  width=\"21\" height=\"17\"/></li>");
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("翻页标签出错！");
		}
		return super.doEndTag();
	}
	
	private String getNextPagePath() {
		String path=getActionPath();
		//将path+="" 换成 path2.append("") qyq 2011-08-1-27 开始
		StringBuffer path2=new StringBuffer();
		path2.append("?queryPage.targetPage="+(queryPage.getTargetPage()+1));
		for(Map.Entry<String,String> param:paramMap.entrySet()) {
			if(!StringUtils.isEmpty(param.getKey())&&!StringUtils.isEmpty(param.getValue())) {
				try {
					path2.append("&"+param.getKey()+"="+java.net.URLEncoder.encode(param.getValue(),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		path+=path2.toString();
		//将path+="" 换成 path2.append("") qyq 2011-08-1-27 结束
		return path;
	}

	private String getPreviousPagePath() {
		String path=getActionPath();
		//将path+="" 换成 path2.append("") qyq 2011-08-1-27 开始
		StringBuffer path2=new StringBuffer();
		path2.append("?queryPage.targetPage="+(queryPage.getTargetPage()-1));
		for(Map.Entry<String,String> param:paramMap.entrySet())
		{
			if(!StringUtils.isEmpty(param.getKey())&&!StringUtils.isEmpty(param.getValue()))
			{
				try {
					path2.append("&"+param.getKey()+"="+java.net.URLEncoder.encode(param.getValue(),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		path+=path2.toString();
		//将path+="" 换成 path2.append("") qyq 2011-08-1-27 结束
		return path;
	}
	
	private String getActionPath()
	{
		String path=pageContext.getServletContext().getContextPath()+namespace+"/"+action+".do";
		return path;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public Map<String, String> getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map<String, String> paramMap) {
		this.paramMap = paramMap;
	}
	
	
}
